/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fmy.jurisdiction.core.commons.factory;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.fmy.jurisdiction.config.spring.SpringContext;
import com.fmy.jurisdiction.core.commons.consts.CacheKey;
import com.fmy.jurisdiction.core.commons.consts.CacheValue;
import com.fmy.jurisdiction.core.commons.kits.MybatisPlusKit;
import com.fmy.jurisdiction.modular.mapper.AdminDeptMapper;
import com.fmy.jurisdiction.modular.mapper.AdminRoleMapper;
import com.fmy.jurisdiction.modular.mapper.AdminUserMapper;
import com.fmy.jurisdiction.modular.model.AdminDept;
import com.fmy.jurisdiction.modular.model.AdminRole;
import com.fmy.jurisdiction.modular.model.AdminUser;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 常量 or 外键 的生产工厂
 *
 * @author fengshuonan
 * @date 2017年2月13日 下午10:55:21
 */
@Component
@DependsOn("springContext")
public class ConstantFactory implements IConstantFactory {

    private AdminUserMapper userMapper = SpringContext.get().getBean(AdminUserMapper.class);

    private AdminRoleMapper roleMapper = SpringContext.get().getBean(AdminRoleMapper.class);

    private AdminDeptMapper deptMapper = SpringContext.get().getBean(AdminDeptMapper.class);

    public static IConstantFactory me() {
        return SpringContext.get().getBean(ConstantFactory.class);
    }

    @Override
    @Cacheable(value = CacheValue.FOREIGN_KEY, key = "'" + CacheKey.USER_NAME_KEY + "'+#id")
    public String getUserName(Integer id) {
        AdminUser user = userMapper.selectById(id);
        return user != null ? user.getUserName() : "";
    }

    @Override
    @Cacheable(value = CacheValue.FOREIGN_KEY, key = "'" + CacheKey.ROLE_NAMES_KEY + "'+#roles")
    public String getRoleNames(String roles) {
        if (StrUtil.isEmpty(roles)) {
            return "";
        }
        List<Object> roleNames = roleMapper.selectObjs(MybatisPlusKit.<AdminRole>q()
                .select(AdminRole.ROLE_NAME)
                .in(AdminRole.ID, Convert.toList(roles)));
        StringBuffer sb = new StringBuffer();
        roleNames.forEach(item -> {
            sb.append(item).append(",");
        });
        return StrUtil.removeSuffix(sb.toString(), ",");
    }

    @Override
    @Cacheable(value = CacheValue.FOREIGN_KEY, key = "'" + CacheKey.DEMP_NAME_KEY + "'+#deptId")
    public String getDeptName(Integer deptId) {
        AdminDept dept = deptMapper.selectById(deptId);
        return dept == null ? "" : dept.getDeptName();
    }
}
